﻿using System;
using System.Collections.Generic;
using PaceR.Core.Interfaces;
using PaceR.Core.Interfaces.Validation;
using PaceR.Core.Utilities;
using PaceR.Core.Interfaces.Conditionals;

namespace PaceR.Core.Validation
{
    public abstract class ValidatableEntity<T> : IValidatableEntity<T>
    {
        public IEnumerable<IConditionRule> Rules { get; protected set; }
        public IResultObjectFactory ResultObjectFactory { get; set; }

        public ValidatableEntity(T entity, IResultObjectFactory resultObjectFactory)
        {
            Entity = entity;
            ResultObjectFactory = resultObjectFactory;
            SetRules();
        }

        public T Entity { get; private set; }

        public IResult<T> RunValidation()
        {
            IResult<T> result = ResultObjectFactory.GetResultObject<T>();
            result.SetResultEntities(Entity);
            foreach (var rule in Rules)
            {
                result.Messages.MergeMessages(rule.Execute());
            }
            return result;
        }

        protected abstract void SetRules();
    }
}
